package com.yzkj.siot.controller;

import com.yzkj.siot.annotation.Permission;
import com.yzkj.siot.annotation.WebLog;
import com.yzkj.siot.base.BaseController;
import com.yzkj.siot.domain.AdminTenant;
import com.yzkj.siot.facade.AuthFacade;
import com.yzkj.siot.req.AdminLoginReq;
import com.yzkj.siot.req.TenantLoginReq;
import com.yzkj.siot.req.TenantRegisterReq;
import com.yzkj.siot.req.TenantRegisterVerifyCodeReq;
import com.yzkj.siot.type.ApiType;
import com.yzkj.siot.type.PermissionType;
import com.yzkj.siot.vo.LoginResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/**
 * 权限相关模块 前端控制器
 *
 * @author skye
 * @since 2021-12-28
 */
@RestController
@RequestMapping("/auth")
@RefreshScope
public class AuthController extends BaseController {

	@Autowired
	AuthFacade authFacade;

	@WebLog(type = ApiType.AUTH)
	@PostMapping("/tenant/register/createVerifyCode")
	public Boolean tenantRegisterCreateVerifyCode(@RequestBody TenantRegisterVerifyCodeReq tenantRegisterVerifyCodeReq) {

		return authFacade.tenantRegisterCreateVerifyCode(tenantRegisterVerifyCodeReq);
	}
	
	@WebLog(type = ApiType.AUTH)
	@PostMapping("/tenant/register")
	public AdminTenant tenantRegister(@RequestBody TenantRegisterReq tenantRegisterReq) {

		return authFacade.tenantRegister(tenantRegisterReq);
	}

	@WebLog(type = ApiType.AUTH)
	@PostMapping("/tenant/login")
	public LoginResponse tenantLogin(@RequestBody TenantLoginReq tenantLoginReq) {

		return authFacade.tenantLogin(tenantLoginReq.getUserName(), tenantLoginReq.getPassword(),
				tenantLoginReq.getTenantAlias());
	}

	@WebLog(type = ApiType.AUTH)
	@PostMapping("/admin/login")
	public LoginResponse adminLogin(@RequestBody AdminLoginReq adminLoginReq) {

		return authFacade.adminLogin(adminLoginReq.getNickName(), adminLoginReq.getPassword());
	}

	@WebLog(type = ApiType.SAVE_OR_UPDATE)
	@Permission(type = PermissionType.TENANT)
	@PostMapping("/tenant/updatePwd")
	public Boolean tenantUpdatePwd(@RequestParam String oldPwd, @RequestParam String newPwd) {

		return authFacade.tenantUpdatePwd(oldPwd, newPwd);
	}

	@WebLog(type = ApiType.READ)
	@PostMapping("getGatewayToken")
	public LoginResponse getGateWayToken(@RequestParam String tenantAlias, @RequestParam String tenantPwd) {

		return authFacade.getGatewayToken(tenantAlias, tenantPwd);
	}
}
